package com.block.test.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.common.bean.SessionBean;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.constant.SmsConstant;
import com.block.common.service.ISysConstantInfoService;
import com.block.common.vo.ResponseVo;
import com.block.test.service.IValidateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SendSmsCodeValidateService extends IValidateService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    ISysConstantInfoService sysConstantInfoService;

    @Override
    public ResponseVo validate(JSONObject jsonObject, SessionBean sessionBean) {

        String operType = jsonObject.getString("operType");
        String mobile = jsonObject.getString("mobile");

        List<String> adminMobileList = sysConstantInfoService.getAdminMobileList();
        if (CollUtil.isNotEmpty(adminMobileList) && adminMobileList.contains(mobile)){
            return new ResponseVo(AppRspCodeConstant.SUCCESS);
        }

        if (!PhoneUtil.isMobile(mobile)){
            return new ResponseVo(AppRspCodeConstant.PARAM_ERROR, "请输入有效手机号", "");
        }

        String mobileStr = SecureUtil.md5(mobile).toLowerCase();
        // 每日发送最大次数
        String countKey = SmsConstant.getCodeCountKey(operType, mobileStr);
        if (stringRedisTemplate.hasKey(countKey)) {
            Long st = Long.parseLong(stringRedisTemplate.opsForValue().get(countKey));
            if (st > 20) {
                return new ResponseVo(AppRspCodeConstant.SMS_SEND_NUM_MAX);
            }
        }

        // 是不是一分钟内又发了短信
        String canSendKey = SmsConstant.getCodeCanSendKey(operType, mobileStr);
        if (stringRedisTemplate.hasKey(canSendKey)) {
            return new ResponseVo(AppRspCodeConstant.SMS_ONE_MINUTE_AFTER_TRY_AGAIN);
        }

        // 该类型短信是否已经发过
//        String key = SmsConstant.getCodeKey(operType, mobile);
//        if (stringRedisTemplate.hasKey(key)) {
//            return new ResponseVo(AppRspCodeConstant.SMS_ALREADY_SEND);
//        }

        // 校验图片验证码的正确性
//        String imageLoginFlagKey = RedisKeyConstant.IMAGE_LOGIN_FLAG;
//        if (stringRedisTemplate.hasKey(imageLoginFlagKey)){
//            String imageCode = jsonObject.getString("imageCode");
//            String validateCode = jsonObject.getString("validateCode");
//            // 需要校验图片验证码
//            String imageRedisKey = RedisKeyConstant.IMAGE_LOGIN_INFO.concat(imageCode + "-" + validateCode);
//            if (!stringRedisTemplate.hasKey(imageRedisKey)){
//                return new ResponseVo(AppRspCodeConstant.SMS_IMAGE_VERIFY_ERROR, "图片验证错误");
//            }
//        }

        // 校验，流量的大小
//        String sendCodeCountKey = RedisKeyConstant.SMS_SEND_COUNT;
//        String countNumber = stringRedisTemplate.opsForValue().get(sendCodeCountKey);
//        if (StrUtil.isNotEmpty(countNumber) && Integer.valueOf(countNumber) > 500){
//            stringRedisTemplate.opsForValue().set(imageLoginFlagKey, "1", 1L, TimeUnit.DAYS);
//        }

        // 添加流量计数缓存
//        if (stringRedisTemplate.hasKey(sendCodeCountKey)){
//            stringRedisTemplate.opsForValue().increment(sendCodeCountKey);
//        }else{
//            stringRedisTemplate.opsForValue().set(sendCodeCountKey, "1", RedisKeyConstant.TIME_FIVE_MINUTES, TimeUnit.MINUTES);
//        }

        return null;
    }
}
